package com.supermap.guangzhou.weifang.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.supermap.guangzhou.weifang.R;
import com.supermap.guangzhou.weifang.activity.PuchaChaxunActivity;
import com.supermap.guangzhou.weifang.activity.PuchaLuruActivity;
import com.supermap.guangzhou.weifang.activity.PuchaTongjiActivity;
import com.supermap.guangzhou.weifang.adapter.PuchaViewAdapter;
import com.supermap.guangzhou.weifang.entity.FangWuAnquanXinXi;
import com.supermap.guangzhou.weifang.utils.NetUtils;
import com.supermap.guangzhou.weifang.utils.ToastUtil;
import com.supermap.guangzhou.weifang.utils.Utils;
import com.supermap.guangzhou.weifang.widget.CustomProgressBar;
import com.supermap.guangzhou.weifang.widget.SwipeLayoutManager;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.StringRequest;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class PuchaFragment extends Fragment {


    Unbinder unbinder;

    List<FangWuAnquanXinXi> list = new ArrayList<>();
    @BindView(R.id.recyclerView)
    XRecyclerView recyclerView;
    @BindView(R.id.pucha_chaxun)
    Button puchaChaxun;
    @BindView(R.id.pucha_tongji)
    Button puchaTongji;
    private String fangwupchaxinxiUrl;
    private PuchaViewAdapter adapter;
    private CustomProgressBar customProgressBar;
    private FangWuAnquanXinXi fangWuAnquanXinXi;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pucha, container, false);
        unbinder = ButterKnife.bind(this, view);
        fangwupchaxinxiUrl = getResources().getString(R.string.fangwupchaxinxi);
        customProgressBar = new CustomProgressBar(Utils.getContext());
        initData();
        initXRecyclerView();
        return view;
    }

    private void initXRecyclerView() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setLoadingMoreEnabled(true);
        recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.Pacman);

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData();
            }

            @Override
            public void onLoadMore() {
                getData();

            }
        });
    }

    private int page = 1;
    private int rows = 10;

    private void initData() {
        list.clear();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
        page = 1;
        StringRequest request = new StringRequest(fangwupchaxinxiUrl);
        request.add("page", page);
        request.add("rows", rows);
        if (fangWuAnquanXinXi != null) {
            request.add("pczt", fangWuAnquanXinXi.getPczt());
            request.add("dcbbh", fangWuAnquanXinXi.getDcbbh());
            request.add("fwbsm", fangWuAnquanXinXi.getFwbsm());
            request.add("yfwjg", fangWuAnquanXinXi.getYfwjg());
            request.add("yzdmj", fangWuAnquanXinXi.getYzdmj());
            request.add("yjzmj", fangWuAnquanXinXi.getYjzmj());
            request.add("sjcs", fangWuAnquanXinXi.getSjcs());
            request.add("fwdz", fangWuAnquanXinXi.getFwdz());
            request.add("mph", fangWuAnquanXinXi.getMph());
            request.add("xzqmc", fangWuAnquanXinXi.getXzqmc());
            request.add("jqmc", fangWuAnquanXinXi.getJqmc());
            request.add("fwxz", fangWuAnquanXinXi.getFwxz());
            request.add("fwcb", fangWuAnquanXinXi.getFwcb());
            request.add("fwzt", fangWuAnquanXinXi.getFwzt());
            request.add("jznd", fangWuAnquanXinXi.getJznd());
            request.add("sfjd", fangWuAnquanXinXi.getSfjd());
            request.add("aqdj", fangWuAnquanXinXi.getAqdj());
            request.add("wxqk", fangWuAnquanXinXi.getWxqk());
            request.add("clyj", fangWuAnquanXinXi.getClyj());
        }
        new NetUtils<Void>(request) {

            @Override
            public Void onSuccess(Response<String> response) {
                boolean succeed = response.isSucceed();
                if (succeed) {

                    String str = response.get();
                    if (!TextUtils.isEmpty(str)) {
                        JSONObject object = JSONObject.parseObject(str);
                        JSONArray rows = object.getJSONArray("rows");
                        List<FangWuAnquanXinXi> fangWuAnquanXinXis = rows.toJavaList(FangWuAnquanXinXi.class);
                        list.addAll(fangWuAnquanXinXis);
                        setAdapter();
                        if (recyclerView != null) {
                            if (adapter != null) {
                                adapter.notifyDataSetChanged();
                            }
                            recyclerView.refreshComplete();
                        }
                    }
                }
                return null;
            }

            @Override
            public Void onFail(Response<String> response) {
                ToastUtil.makeText(Utils.getContext(), "联网失败了...");
                return null;
            }
        };
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void getData() {
        page++;
        StringRequest request = new StringRequest(fangwupchaxinxiUrl);
        request.add("page", page);
        request.add("rows", rows);
        if (fangWuAnquanXinXi != null) {
            request.add("pczt", fangWuAnquanXinXi.getPczt());
            request.add("dcbbh", fangWuAnquanXinXi.getDcbbh());
            request.add("fwbsm", fangWuAnquanXinXi.getFwbsm());
            request.add("yfwjg", fangWuAnquanXinXi.getYfwjg());
            request.add("yzdmj", fangWuAnquanXinXi.getYzdmj());
            request.add("yjzmj", fangWuAnquanXinXi.getYjzmj());
            request.add("sjcs", fangWuAnquanXinXi.getSjcs());
            request.add("fwdz", fangWuAnquanXinXi.getFwdz());
            request.add("mph", fangWuAnquanXinXi.getMph());
            request.add("xzqmc", fangWuAnquanXinXi.getXzqmc());
            request.add("jqmc", fangWuAnquanXinXi.getJqmc());
            request.add("fwxz", fangWuAnquanXinXi.getFwxz());
            request.add("fwcb", fangWuAnquanXinXi.getFwcb());
            request.add("fwzt", fangWuAnquanXinXi.getFwzt());
            request.add("jznd", fangWuAnquanXinXi.getJznd());
            request.add("sfjd", fangWuAnquanXinXi.getSfjd());
            request.add("aqdj", fangWuAnquanXinXi.getAqdj());
            request.add("wxqk", fangWuAnquanXinXi.getWxqk());
            request.add("clyj", fangWuAnquanXinXi.getClyj());
        }
        new NetUtils<Void>(request) {

            @Override
            public Void onSuccess(Response<String> response) {
                boolean succeed = response.isSucceed();
                if (succeed) {
                    String str = response.get();
                    if (!TextUtils.isEmpty(str)) {
                        JSONObject object = JSONObject.parseObject(str);
                        JSONArray rows = object.getJSONArray("rows");
                        List<FangWuAnquanXinXi> fangWuAnquanXinXis = rows.toJavaList(FangWuAnquanXinXi.class);
                        list.addAll(fangWuAnquanXinXis);
                        if (recyclerView != null) {
                            if (adapter != null) {
                                adapter.notifyDataSetChanged();
                            }
                            recyclerView.loadMoreComplete();
                        }
                    }
                }
                return null;
            }

            @Override
            public Void onFail(Response<String> response) {
                ToastUtil.makeText(Utils.getContext(), "联网失败了...");
                return null;
            }
        };


    }


    private void setAdapter() {
        if (adapter == null && recyclerView != null) {
            adapter = new PuchaViewAdapter(list);
            recyclerView.setAdapter(adapter);
            adapter.setOnPuchaListener(new PuchaViewAdapter.OnPuchaListener() {
                @Override
                public void onPucha(FangWuAnquanXinXi fangWuAnquanXinXi) {
                    if ("未普查".equals(fangWuAnquanXinXi.getPczt())) {
                        Intent intent = new Intent(Utils.getContext(), PuchaLuruActivity.class);
                        intent.putExtra("bean", fangWuAnquanXinXi);
                        startActivity(intent);
                    } else {
                        ToastUtil.makeText(Utils.getContext(), "该房屋已经普查，请普查未普查的房屋...");
                    }
                }
            });
        }
    }


    @OnClick({R.id.pucha_chaxun, R.id.pucha_tongji})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pucha_chaxun:
                Intent intent = new Intent(Utils.getContext(), PuchaChaxunActivity.class);
                startActivityForResult(intent, 200);
                break;
            case R.id.pucha_tongji:
                intent = new Intent(Utils.getContext(), PuchaTongjiActivity.class);
                startActivityForResult(intent,300);
                break;
            default:
                break;
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        SwipeLayoutManager.getInstance().closeCurrentLayout();
        SwipeLayoutManager.getInstance().clearCurrentLayout();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 200) {
            fangWuAnquanXinXi = PuchaChaxunActivity.fangWuAnquanXinXi;
            if (fangWuAnquanXinXi != null) {
                initData();
            }


        }else if(requestCode == 300){
            initData();
        }
    }
}
